<template>
  <div class="model-container">
    <div class="listName">属性组管理</div>
    <!--搜索-->
    <el-card shadow="never">
      <div slot="header" class="clearfix">
        <span>快捷搜索</span>
        <div class="btns-wrapper-right">
          <el-button v-if="!searchShow" icon="el-icon-arrow-down" @click="searchShow = true"/>
          <el-button v-else icon="el-icon-arrow-up" @click="searchShow = false"/>
        </div>
      </div>
      <el-collapse-transition>
        <div v-show="searchShow" class="cardbody">
          <el-form ref="seacherParam" :model="seacherParam">
            <el-row :gutter="10">
              <el-col :xl="5" :lg="6" :md="6" :sm="8" >
                <el-col :xl="7" :lg="7" :md="8" :sm="8" class="input-name">属性组名称</el-col>
                <el-col :xl="17" :lg="17" :md="16" :sm="16">
                  <el-form-item prop="">
                    <el-input size="small" placeholder="请输入属性组名称"/>
                  </el-form-item>
                </el-col>
              </el-col>
              <el-col :xl="5" :lg="6" :md="6" :sm="8" >
                <el-col :xl="7" :lg="7" :md="8" :sm="8" class="input-name">是否可用</el-col>
                <el-col :xl="17" :lg="17" :md="16" :sm="16">
                  <el-form-item prop="">
                    <el-select value="0" placeholder="请选择">
                      <el-option label="全部" value="0"/>
                      <el-option label="是" value="1"/>
                      <el-option label="否" value="2"/>
                    </el-select>
                  </el-form-item>
                </el-col>
              </el-col>
            </el-row>
            <div class="cardfooter">
              <el-button type="primary" size="mini" plain @click="seachFun">查询</el-button>
              <el-button type="warning" size="mini" plain @click="resetFun">重置</el-button>
            </div>
          </el-form>
        </div>
      </el-collapse-transition>
    </el-card>
    <!--表格-->
    <el-card style="margin-top: 10px">
      <div slot="header" class="clearfix">
        <div class="btns-wrapper-left">
          <el-button type="primary" plain @click="add">新增</el-button>
          <el-button type="danger" plain @click="add">删除</el-button>
        </div>
      </div>
      <div class="table-wrapper">
        <el-table v-loading="!dataOption" :stripe="true" :data="dataOption" border empty-text="暂无数据" @selection-change="changeFun">

          <el-table-column align="center" type="selection" width="55"/>

          <el-table-column type="expand">
            <template slot-scope="props">
              <el-form label-position="left" inline>
                <el-form-item label="商品名称">
                  <span>{{ props.row.name }}</span>
                </el-form-item>
                <el-form-item label="所属店铺">
                  <span>{{ props.row.name }}</span>
                </el-form-item>
                <el-form-item label="商品 ID">
                  <span>{{ props.row.name }}</span>
                </el-form-item>
                <el-form-item label="店铺 ID">
                  <span>{{ props.row.name }}</span>
                </el-form-item>
                <el-form-item label="商品分类">
                  <span>{{ props.row.name }}</span>
                </el-form-item>
                <el-form-item label="店铺地址">
                  <span>{{ props.row.address }}</span>
                </el-form-item>
                <el-form-item label="商品描述">
                  <span>{{ props.row.name }}</span>
                </el-form-item>
              </el-form>
            </template>
          </el-table-column>

          <el-table-column align="center" prop="propName" label="名称"/>

          <el-table-column align="center" label="是否可用">
            <template slot-scope="scope">{{ isSellingPropVal(scope.row.isDeleted) }}</template>
          </el-table-column>

          <el-table-column align="center" label="输入类型">
            <template slot-scope="scope">{{ inputTypeVal(scope.row.inputType) }}</template>
          </el-table-column>

          <el-table-column align="center" label="销售属性">
            <template slot-scope="scope">{{ isSellingPropVal(scope.row.isSellingProp) }}</template>
          </el-table-column>

          <el-table-column align="center" label="属性选项图片">
            <template slot-scope="scope">{{ isOptionPicAvailableVal(scope.row.isOptionPicAvailable) }}</template>
          </el-table-column>

          <el-table-column align="center" label="SKU图片组">
            <template slot-scope="scope">{{ isSkuPicAvailableVal(scope.row.isSkuPicAvailable) }}</template>
          </el-table-column>

          <el-table-column align="center" label="操作" width="200px">
            <template slot-scope="scope">

              <el-button title="编辑" type="primary" circle icon="el-icon-edit" class="table-icon" @click="edit(scope.row)"/>

              <el-button title="删除" type="warning" circle icon="el-icon-delete" class="table-icon" @click="del(scope.row)"/>

            </template>
          </el-table-column>

        </el-table>

        <!--分页-->
        <el-pagination
          :current-page="pageNo"
          :page-sizes="[10, 20, 30]"
          :page-size="pageSize"
          :total="rowCount"
          background
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"/>
      </div>
    </el-card>

    <!--弹框-->
    <el-dialog
      :title="dialogTitle"
      :visible.sync="dialogState"
      :close-on-click-modal="false"
      :before-close="handleClose"
      top="5vh"
      width="30%">
      <el-form ref="dialogBindData" :model="dialogBindData" :rules="rules" label-width="30%" status-icon class="demo-ruleForm">
        <div class="formBody">
          <el-form-item label="属性组名称" prop="propName">
            <el-input v-model="dialogBindData.propName" size="small" type="text" placeholder="请输入属性组名称"/>
          </el-form-item>
          <el-form-item label="语言" prop="language">
            <el-select v-model="dialogBindData.language" placeholder="请选择" size="small">
              <el-option label="zh_CN" value="1"/>
            </el-select>
          </el-form-item>
          <el-form-item label="参数描述" prop="memo">
            <el-input v-model="dialogBindData.memo" autosize type="textarea" placeholder="备注"/>
          </el-form-item>
          <el-form-item v-if="dialogType !== 1" label="是否锁定" prop="">
            <el-radio-group>
              <el-radio :label="1">是</el-radio>
              <el-radio :label="2">否</el-radio>
            </el-radio-group>
          </el-form-item>
        </div>
        <div class="btns-wrapper">
          <el-button size="small" @click="handleClose">取 消</el-button>
          <el-button :loading="buttonState" type="primary" size="small" @click="submmitFun">保 存</el-button>
        </div>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import * as commodity from '../../api/commodity'
export default {
  data() {
    return {
      // 页面搜索
      seacherParam: {
        propName: ''
      },
      searchShow: false,
      pageNo: 1,
      pageSize: 10,
      rowCount: 20, // 总条数
      // 弹框
      dialogType: 1, // 1--增加  2--编辑  3--查看
      dialogState: false,
      // 按钮状态
      buttonState: false,
      // 列表数据
      dataOption: null,
      datavalID: [],
      // 提交内容
      dialogBindData: {
        propName: '',
        language: '1',
        memo: ''
      },
      rules: {
        propName: [
          { required: true, message: '请输入属性组名称', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    dialogTitle() {
      if (this.dialogType === 1) {
        return '新增商品属性组'
      }
      if (this.dialogType === 2) {
        return '编辑商品属性组'
      } else {
        return '查看'
      }
    }
  },
  beforeMount() {
    this.getList()
  },
  methods: {
    seachFun() {

    },
    getList(cb) {
      const param = {
        current: this.pageNo,
        size: this.pageSize
      }
      commodity.getAttrGroupList(param).then(res => {
        if (res.data.code !== 1) {
          this.$message.error(res.data.msg)
          return false
        }
        if (res.data.data === 'null' || res.data.data === null) {
          return false
        }
        // 初始话表格数据
        this.dataOption = res.data.data.records
        // 初始化分页数据
        this.rowCount = res.data.data.total
        this.pageSize = res.data.data.size
      })
    },
    // 新增
    add() {
      this.dialogType = 1
      this.dialogState = true
    },
    // 编辑
    edit(data) {
      this.dialogType = 2
      this.dialogState = true
      this.reBindData(data)
    },
    // 提交
    submmitFun() {
      const param = {}

      if (this.dialogType !== 1) {
        param.id = this.dialogBindData.id
      }
      this.$refs.dialogBindData.validate((valid) => {
        if (valid) {
          if (this.dialogType === 1) {
            commodity.addAttrGroupList(param).then(res => {
              if (res.data.code !== 1) {
                this.$message({
                  message: res.data.msg,
                  type: 'warning'
                })
                this.buttonState = false
                return false
              }
              this.$message({
                message: '添加成功',
                type: 'success'
              })
              this.buttonState = false
              this.getList()
              this.handleClose()
            })
          } else {
            commodity.editorAttrGroupList(param).then(res => {
              if (res.data.code !== 1) {
                this.$message({
                  message: res.data.msg,
                  type: 'warning'
                })
                this.buttonState = false
                return false
              }
              this.$message({
                message: '编辑成功',
                type: 'success'
              })
              this.buttonState = false
              this.getList()
              this.handleClose()
            })
          }
        } else {
          return false
        }
      })
    },
    // 删除
    changeFun(data) {
      this.datavalID = data
    },
    del(data) {
      if (data === 0) {
        if (this.datavalID.length === 0) {
          this.$message({
            type: 'warning',
            message: '请选择商家!'
          })
        }
        return false
      }
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.delList(data)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    delList(data) {
      const classID = []
      if (data === 0) {
        for (const val of this.datavalID) {
          classID.push(val.catId)
        }
      } else {
        classID.push(data.catId)
      }
      commodity.delBrandList(classID).then(res => {
        if (res.data.code === 1) {
          this.$message({
            message: '删除成功',
            type: 'success'
          })
          this.getList()
        }
      })
    },
    // 反显
    reBindData(data) {
      for (const key in this.dialogBindData) {
        this.dialogBindData[key] = data[key]
      }
    },
    // 重置
    resetFun() {
      this.$refs.seacherParam.resetFields()
      this.pageNo = 1
      this.pageSize = 10
      this.getList()
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.pageSize = val
      this.getList()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.pageNo = val
      this.getList()
    },
    handleClose() {
      for (const key in this.dialogBindData) {
        if (key === 'language') {
          this.dialogBindData[key] = '1'
        } else {
          this.dialogBindData[key] = ''
        }
      }
      this.dialogState = false
      this.$refs.dialogBindData.clearValidate()
    },
    // 输入类型
    inputTypeVal(data) {
      if (data === 0) {
        return '单选'
      } else if (data === 1) {
        return '多选'
      } else if (data === 2) {
        return '手工输入'
      }
    },
    // 是否销售属性 || 是否可用
    isSellingPropVal(data) {
      if (data === 0) {
        return '否'
      } else if (data === 1) {
        return '是'
      }
    },
    // 是否允许属性选项带有图片
    isOptionPicAvailableVal(data) {
      if (data === 0) {
        return '不允许'
      } else if (data === 1) {
        return '允许'
      }
    },
    // 是否允许sku有自己的图片组
    isSkuPicAvailableVal(data) {
      if (data === 0) {
        return '不允许'
      } else if (data === 1) {
        return '允许'
      }
    }
  }
}
</script>
<style type="text/scss" lang="scss" scoped>

</style>
